package com.haidechizi.i18n.common.utils;

import com.alibaba.fastjson.JSON;
import com.haidechizi.i18n.common.api.vo.SingleLanguage;
import com.haidechizi.i18n.common.cache.CacheService;
import com.haidechizi.i18n.common.config.Configuration;
import com.haidechizi.i18n.common.executor.CacheExecutor;
import com.haidechizi.i18n.common.executor.Executor;
import com.haidechizi.i18n.common.translate.Translator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 初始化applicationContext容器
 * 初始化i18nService，用于获取国际化的数据
 */
@Slf4j
public class I18nInfoUtils implements ApplicationContextAware, InitializingBean {

    private ApplicationContext applicationContext;

    @Autowired
    private Executor executor;

    @Autowired
    private Configuration configuration;

    public Translator getTranslator(String metaCalculator) {
        return applicationContext.getBean(metaCalculator, Translator.class);
    }

    public Map<String, Map<String, String>> queryLocaleInfo(String systemCode, String businessCode,
                                                            String languageName, List<String> businessKeyList) {
        if (StringUtils.isEmpty(systemCode) || StringUtils.isEmpty(businessCode) || StringUtils.isEmpty(languageName)
                || CollectionUtils.isEmpty(businessKeyList)) {
            return Collections.emptyMap();
        }
        Map<String, Map<String, String>> result = new HashMap<>();
        List<SingleLanguage> singleLanguageList = executor.queryLocaleInfo(systemCode, businessCode, languageName,
                businessKeyList);
        singleLanguageList.forEach(singleLanguage -> {
            try {
                Map map = JSON.parseObject(singleLanguage.getDocJson(), Map.class);
                result.put(singleLanguage.getBusinessKey(), map);
            } catch (Exception e) {
                log.error("国际化数据格式错误,languageName{},data:{}", languageName, singleLanguage, e);
            }
        });
        return result;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if (configuration.isI18nUseCache()) {
            CacheService cacheService = applicationContext.getBean(CacheService.class);
            executor = new CacheExecutor(executor, cacheService, configuration.getI18nUseCacheExpireTime());
            log.info("查询国际化服务启用缓存成功:{}", executor);
        }
    }
}
